package blackitheima.Rounnable;

//购票事件
public class MyRounnable implements Runnable {
    private static int tickCount = 100;

    @Override
    public void run() {
        while (true) {
            //同步方法
            if ("窗口一".equals(Thread.currentThread().getName())) {
                //创建同步方法
                boolean result = synchronizedDemo();
                if (result) {
                    break;
                }
            }

            //同步代码块
            if ("窗口二".equals(Thread.currentThread().getName())) {
                synchronized (this) {
                    if (tickCount == 0) {
                        break;
                    } else {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        tickCount--;
                        System.out.println(Thread.currentThread().getName() + "在卖票,还剩" + tickCount + "张票");
                    }
                }

            }
        }

    }

    private synchronized boolean synchronizedDemo() {
        if (tickCount == 0) {
            return true;
        } else {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tickCount--;
            System.out.println(Thread.currentThread().getName() + "在卖票,还剩" + tickCount + "张票");
            return false;
        }
    }
}
